import path from 'path';
import fs from 'fs';
import mkdirSync from './mkdirSync.mjs';

/**
 * 复制目录
 * @param {string} sourceDirPath 源目录路径
 * @param {string} destDirPath 目标目录路径
 */
export default function copyDirSync(sourceDirPath, destDirPath) {
    if (!fs.existsSync(sourceDirPath)) { return; }
    if (!fs.statSync(sourceDirPath).isDirectory()) { return; }
    if (!fs.existsSync(destDirPath)) { mkdirSync(destDirPath); }

    fs.readdirSync(sourceDirPath).forEach(filename => {
        const sourceFilePath = path.join(sourceDirPath, filename);
        const destFilePath = path.join(destDirPath, filename);
        const stats = fs.statSync(sourceFilePath);
        if (stats.isFile()) {
            fs.copyFileSync(sourceFilePath, destFilePath);
        } else if (stats.isDirectory()) {
            copyDirSync(sourceFilePath, destFilePath);
        }
    });
}
